package cn.hongye.controller;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.hongye.common.JsonResult;
import cn.hongye.model.User;
import cn.hongye.service.UserService;

@Controller
public class UserController {

    private Logger logger = Logger.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    @RequestMapping("/getUserInfo")
    @ResponseBody
    public User getUserInfo() {
//    	int i = 1/0;
//    	throw new BusinessException("10", "/ by zero");
        User user = userService.getUserInfo();
        if(user!=null){
            System.out.println("user.getName():"+user.getUsername());
            logger.info("user.getAge():"+user.getAge());
        }
        return user;
    }
    
    //登录处理
    @RequestMapping(value="/login", method= RequestMethod.GET)
    public JsonResult login(@RequestParam(value = "username",required = false)String username,
                            @RequestParam(value = "password",required = false)String password){
        try {
            User user=new User();
            user.setUsername(username);
            user.setPassword(password);
            User user1=userService.selectByUsername(user);
            return new JsonResult("200", "登录成功",user1);
        }catch (Exception e){

            return new JsonResult("500",e.getMessage());
        }
    }
    
}
